/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2024-11-02
 * Time:21:05
 */
public class MyQueue {
    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode front;//队头
    private ListNode rear;//队尾
    private int usedSize;

    //队尾入队
    public void offer(int x) {
        ListNode node = new ListNode(x);
        if (front == null) {
            front = rear = node;
        } else {
            rear.next = node;
            node.prev = rear;
            rear = node;
        }
        usedSize++;
    }

    //队尾删除
    public int poll() {
        //队为空
        if (front == null) {
            return -1;
        }
        int ret = front.val;
        //队里只有一个元素
        if (front == rear) {
            front = null;
            rear = null;
            usedSize--;
            return -1;
        }
        front = front.next;
        front.prev = null;
        usedSize--;
        return ret;
    }

    //获取队头的第一个节点的元素
    public int peek() {
        if (front == null) {
            return -1;
        }
        return front.val;
    }

    //获取元素个数
    public int getUsedSize() {
        return usedSize;
    }

    //判空
    public boolean isEmpty() {
        if (usedSize == 0){
            return true;
        }
        return false;
    }
}
